package com.topfinance.xml;

import java.util.Hashtable;

public class MsgTypeFactory {
	//消息和处理类对照表，是否需要定义到配置文件中？
	public static final String ALL_MSG_TYPES[][] = new String[][] {
		{ "com.topfinance.xml.pacs.v00800102", "ibps.101.001.01", "FIToFICstmrCdtTrf",
			"com.topfinance.xml.handler.ibps.Msg101Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00200103", "ibps.102.001.01", "FIToFIPmtStsRpt",
			"com.topfinance.xml.handler.ibps.Msg102Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00300102", "ibps.103.001.01", "FIToFICstmrDrctDbt",
			"com.topfinance.xml.handler.ibps.Msg103Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00200103", "ibps.104.001.01", "FIToFIPmtStsRpt",
			"com.topfinance.xml.handler.ibps.Msg104Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00800102", "ibps.105.001.01", "FIToFICstmrCdtTrf",
			"com.topfinance.xml.handler.ibps.Msg105Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00200103", "ibps.106.001.01", "FIToFIPmtStsRpt",
			"com.topfinance.xml.handler.ibps.Msg106Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.camt.v05600101", "ibps.307.001.01", "FIToFIPmtCxlReq",
			"com.topfinance.xml.handler.ibps.Msg307Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.pacs.v00200103", "ibps.308.001.01", "FIToFIPmtStsRpt",
			"com.topfinance.xml.handler.ibps.Msg308Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.camt.v05400102", "saps.601.001.01", "BkToCstmrDbtCdtNtfctn",
			"com.topfinance.xml.handler.ibps.Msg601Handler","IO","10","GNRL"},
		{ "com.topfinance.xml.camt.v05300102", "saps.603.001.01", "BkToCstmrStmt",
			"com.ecados.svc.cnaps2.xml.handler.Msg603Handler", "I" ,"10","RTIME"} };
		
	private static MsgTypeFactory instance = null;
	private static Hashtable msgTypes = new Hashtable();
	private static Hashtable msgPkgs = new Hashtable();
	
	private void MsgTypeFactory(){
	}
	
	public static MsgTypeFactory getInstance() throws Exception{
		if(instance == null){
			instance = new MsgTypeFactory();
			instance.initArray();
		}
		return instance;
	}
	
	private static void initArray() throws Exception{
		for (int i = 0; i < ALL_MSG_TYPES.length; i++) {
			MsgType msg = new MsgType(ALL_MSG_TYPES[i]);
			msgTypes.put(msg.getMsgType(),msg);
			msgPkgs.put(msg.getMsgPkg(),msg);
		}
	}
	
	public static String getPackageByMsgId(String msgId) {
		MsgType msgType = (MsgType)msgTypes.get(msgId);
		if(msgType != null)
			return msgType.getMsgPkg();
		else
			return null;
	}
	
	public static String getPackageByXML(String xmlDataStr) {
		int index;
		for (int i = 0; i < ALL_MSG_TYPES.length; i++) {
			index = xmlDataStr.indexOf("<" + ALL_MSG_TYPES[i][2] + ">");
			if (index > -1) {
				String preDataStr = xmlDataStr.substring(0, index);
				if (preDataStr.trim().endsWith("<Document>"))
					return ALL_MSG_TYPES[i][0];
			}
		}
		return null;
	}	
	
	public static Class getBizLogicClassByPackageName(String packageName) throws Exception {
		MsgType msgType = (MsgType)MsgTypeFactory.msgPkgs.get(packageName);
		if(msgType != null)
			return MsgHandlerFactory.class.getClassLoader().loadClass(msgType.getMsgHandler());
		else
			return null;
	}
	
	public static Class getBizLogicClassByMsgId(String msgId) throws Exception {
		MsgType msgType = (MsgType)msgTypes.get(msgId);
		if(msgType != null)
			return MsgHandlerFactory.class.getClassLoader().loadClass(msgType.getMsgHandler());
		else
			return null;
	}
	
	public static String getMsgIdByPackageName(String packageName) {
		MsgType msgType = (MsgType)msgPkgs.get(packageName);
		if(msgType != null)
			return msgType.getMsgPkg();
		else
			return null;
	}
}
